<%@page language="C#" Debug="true" %>
<%@import Namespace="System.Security.Principal"%>
<%@import Namespace="MAF.Authentication" %>
<%@import Namespace="MAF.Authentication.Entities" %>
<%@import Namespace="MAF.Authentication.Service" %>

<script runat="server">
    protected void Page_Load( object sender, EventArgs e )
    {
        PnlLogin.Visible = true;
        PnlLogout.Visible = false;
            
        string op = Request.QueryString["op"];
        if( !string.IsNullOrEmpty(op) )
            op = op.ToLower();

        if( "login"==op )
        {
            Login();
        }
        else if( "logout"==op )
        {
            PnlLogout.Visible = true;
            //Logout();
        }
    }

    public void Login()
    {
        
        string username = HttpContext.Current.Request.Form["Username"];
        string password = HttpContext.Current.Request.Form["Password"];
        string remember = HttpContext.Current.Request.Form["RememberMe"];
        string from = HttpContext.Current.Request.Form["From"];


        try
        {
            MAFIdentity id = new MAFIdentity(UserIdType.Email,username,password);
            WebAuthenticationManager.CreatePrincipal(id);

        }
        catch (Exception e)
        {
            Response.Write(ErrMsg(e.Message));
            Response.End();
        }

        //string uid = Guid.NewGuid().ToString("N");
        //HttpContext.Current.Session[uid] = id;
        //HttpContext.Current.Response.Cookies["uid"].Value = uid;

        ////HttpContext.Current.Response.Redirect( "app.aspx" );
        //if (string.IsNullOrEmpty(from) || "/" == from)
        //    from = "app.aspx";
        HttpContext.Current.Response.Write("{'msg':'登录成功','redirect':'" + from + "'}");
        Response.End();
    }
    //public void Logout()
    //{
    //    HttpCookie ck = HttpContext.Current.Request.Cookies["uid"];
    //    if (null != ck)
    //    {
    //        Session[ck.Value] = null;
    //        HttpContext.Current.Request.Cookies["uid"].Value = "";
    //    }
    //}

    public string ErrMsg(string message)
    {
        return string.Format("{{'error':'{0}'}}", MAF.Common.Convert.ToJsonParam(message));
    }
    private string Msg(string message)
    {
        return string.Format("{{msg:'{0}'}}", MAF.Common.Convert.ToJsonParam(message));
    }
</script>
<%
  // 这些是旧的退出功能
  //int tdcSessionId = 0;
  //if (null != Request.Cookies["TdcSessionId"])
  //    tdcSessionId = Text.StringToInt(Request.Cookies["TdcSessionId"].Value);
      
  //bool b = SysApi.user.LogoutWebUser(tdcSessionId);
  
  //if (true == b)
  //{
  //    Response.Write("退出成功");
  //    // 3秒之后跳转, 暂未实现
  //    Response.Redirect("index_cn.aspx");
  //}
  //else
  //{
  //    Response.Write ("退出失败,没有找到已登陆用户!请检查您是否已经登陆!");
  //}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <title> 用户登陆 </title>
    <meta http-equiv="Content-Type" Content="text/html;charset=utf-8" />
    <meta name="Version" content="0.0.0.0" />
    <meta name="Copyright" content="ML" />
    <meta name="LastModify" content="2008-12-03" />
    <link type="text/css" rel="stylesheet" href="css/login.css" />
</head>


<body>
    <div id="header">
    </div><!--//#header -->


    <div id="column-A">
    </div><!--//#column-A -->


    <div id="column-B">
    <asp:Panel ID="PnlLogin" runat="server">
      <div id="rbac_login">
          <h3>登陆</h3>
          <form class="frm" method="post" action="login.aspx?op=login">
              <br /><label for="name">帐号</label>:<input id="name" name="Username" type="text"  />
              <br /><label for="pass">密码</label>:<input id="pass" name="Password" type="password" />
              <br /><input id="RememberMe" name="RememberMe" type="checkbox" /><label for="RememberMe">在此计算机上保存我的信息。</label>
              <br /><input name="From" type="hidden" value=<%=Request.QueryString["From"]%> />
              <br /><input type="submit" value="登陆" />
          </form>

          <p style="line-height:1.5em;background:#ccc;border:1px solid #999;margin-top:10px;padding:3px;">
  	        <!--说明:本网站完全基于ajax而建立,其中用户界面使用了大量javascript,所以使用本网站时javascript和cookie功能必须打开,而且请使用符合w3c标准的现代浏览器阅读本网站,例如IE7+,FireFox3+,Chrome1+(谷歌浏览器)等。-->
  	        建议使用符合w3c标准的现代浏览器阅读本网站,例如IE7+,FireFox3+,Chrome1+(谷歌浏览器)等。
  	        <span style="color:red;">注意:在ie6下,本网站可能会有一些功能不能正常使用。</span>
          </p>
      </div>
    </asp:Panel>
    <asp:Panel ID="PnlLogout" runat="server">
        成功退出！
    </asp:Panel>
    </div><!--//#column-B -->


    <script type="text/javascript" src="js/jquery.pack.js"></script>
    <script type="text/javascript" src="js/myjslib.js"></script>


  <script type="text/javascript">

      $(function(){
          Login();
      });

      function Login()
      {
          var frm = $("#rbac_login").find(".frm");
          if( 0 == frm.length ){  alert( "" );     }
          frm.submit( function(){
              $.ajax({
                  async: false,
                  cache: false,
                  timeout: 20000,
                  type: "POST",
                  contentType:"application/x-www-form-urlencoded",

                  url: $(this).attr("action"),
                  data:$(this).serialize(),
                  dataType: 'json',

                  success:function(dat){
                      if( dat.msg )
                      {
                        location.href=dat.redirect;
                      }
                      else
                      {
                        alert( dat.error );
                      }
                  },
                  error: function(dat){ alert(dat.responseText + ": Error, 发生了错误");  }
              });
              return false;
          });
      }


  </script>

</body>
</html>


